/**
 * Created by showtan on 2018/12/27.
 * 个人页面
 */

//网络图片地址
// var imgURL = "http://www.hangge.com/blog/images/logo.png";
// import RNFS from 'react-native-fs';

// import Modal from 'react-native-modalbox';
// import JPushModule from 'jpush-react-native';
import ParallaxScrollView from 'react-native-parallax-scroll-view';
class MinePage extends Component {
  constructor(props) {
    super(props);
    this.state = {
      // lat: 0.001
      isLogin: Math.ceil(Math.random() * 2) > 1,
      num: 0,
      scrollY: new Animated.Value(0)
    }
  }
  componentDidMount() {
    // this.intervalId = BackgroundTimer.setInterval(() => { // 多个后台任务
    //   // this will be executed every 200 ms
    //   // even when app is the the background
    //   console.log('tic');
    // }, 1000);

    // BackgroundTimer.runBackgroundTimer(() => { this.setState({ num: this.state.num + 1 }); }, 3000); // 后台任务
  }
  // async saveImg(img) { // 图片保存
  //   if (isIOS) {
  //     var promise = CameraRoll.saveToCameraRoll(img);
  //     promise.then(result => {
  //       Toast.success('保存成功！');
  //     }).catch(error => {
  //       Toast.fail('保存失败！');
  //     });
  //   } else {
  // try {
  //   const grantedCamera = await PermissionsAndroid.request(
  //     PermissionsAndroid.PERMISSIONS.CAMERA, // 相机权限
  //     { 'title': '我要相机权限', 'message': '没权限我不能工作，同意就好了' }
  //   );
  //   if (grantedCamera === PermissionsAndroid.RESULTS.GRANTED) {
  // try {
  //   const granted = await PermissionsAndroid.request(
  //     PermissionsAndroid.PERMISSIONS.WRITE_EXTERNAL_STORAGE, // 存储权限
  //     { 'title': '保存图片需要获取存储权限', 'message': '是否同意' }
  //   );
  //   console.log(granted, PermissionsAndroid.RESULTS.GRANTED)
  //   if (granted === PermissionsAndroid.RESULTS.GRANTED) {
  //     const storeLocation = `${RNFS.DocumentDirectoryPath}`;
  //     let pathName = new Date().getTime() + "文件名.png";
  //     let downloadDest = `${storeLocation}/${pathName}`;
  //     const ret = RNFS.downloadFile({ fromUrl: img, toFile: downloadDest });
  //     ret.promise.then(res => {
  //       if (res && res.statusCode === 200) {
  //         var promise = CameraRoll.saveToCameraRoll("file://" + downloadDest);
  //         promise.then(result => {
  //           Toast.success('保存成功！');
  //         }).catch(error => {
  //           console.log(error)
  //           Toast.fail('保存失败！');
  //         });
  //       }
  //     });
  //   } else {
  //     Toast.fail('存储权限未开启，保存失败！');
  //   }
  // } catch (err) {
  //   Toast.fail(err.toString());
  // }
  //   } else {
  //     Toast.fail('存储权限未开启，保存失败！');
  //   }
  // } catch (err) {
  //   Toast.fail(err.toString());
  // }
  // }
  // }
  componentWillUnmount() {
    // Cancel the timer when you are done with it
    // BackgroundTimer.clearInterval(this.intervalId);
    BackgroundTimer.stopBackgroundTimer();
  }
  // doSheet = () => {
  //   ActionSheetIOS.showActionSheetWithOptions({
  //     options: [
  //       '拨打电话',
  //       '发送邮件',
  //       '发送短信',
  //       '取消'
  //     ],
  //     cancelButtonIndex: 3,
  //     destructiveButtonIndex: 0
  //   },
  //     (index) => {
  //       alert('您点击了:' + index);
  //     }
  //   );
  // }

  // doShare = () => {
  //   ActionSheetIOS.showShareActionSheetWithOptions({
  //     url: ''
  //   },
  //     (error) => {
  //       alert(error)
  //     },
  //     (e) => {
  //       alert(e)
  //     }
  //   );
  // }
  onClose = () => {
    console.log(1)
  }
  // sendMessage = () => { // 发送消息
  //   var currentDate = new Date()
  //   JPushModule.sendLocalNotification({
  //     id: 5,
  //     content: 'content',
  //     extra: { key1: 'value1', key2: 'value2' },
  //     fireTime: currentDate.getTime() + 3000,
  //     badge: 1,
  //     // sound: 'fasdfa',
  //     subtitle: 'subtitle',
  //     title: 'title'
  //   })
  // }
  render() {
    let data = [{ name: '关注', num: this.state.isLogin ? 11 : 0 }, { name: '粉丝', num: this.state.isLogin ? 11 : 0 }, { name: '访问', num: this.state.isLogin ? 11 : 0 }];
    let infoData = [
      { title: '消息通知', onPress: () => { } },
      { title: '我的收藏', onPress: () => { } },
      { title: '头条商城', onPress: () => { } },
      { title: '我要爆料', onPress: () => { } },
      { title: '用户反馈', onPress: () => { } },
      { title: '系统设置', onPress: () => { } },
    ];
    return (
      <View style={styles.container}>
        <View style={{ flex: 1 }}>
          <ParallaxScrollView
            style={{ flex: 1, backgroundColor: '#ededed', overflow: 'hidden' }}
            renderBackground={() => <ImageBackground source={Images.mine.icon_wode_bg} style={{ height: adapt(250), width: width, alignItems: 'center' }} />}
            renderForeground={() => <TouchableOpacity style={{ height: adapt(250), width: width, alignItems: 'center' }} onPress={() => this.props.navigation.navigate(this.state.isLogin ? 'UpdateInfo' : 'LoginScreen', {})} activeOpacity={.9}>
              <Image source={this.state.isLogin ? Images.other.icon_flatlist : Images.other.icon_admin} style={styles.avatarStyle} />
              <Text style={{ color: '#fff', fontSize: adapt(22), fontWeight: '600', marginTop: adapt(10) }}>{this.state.isLogin ? '刘大帅' : '未登录'}</Text>
              <Text style={{ color: '#fff', fontSize: adapt(18), marginTop: adapt(10) }}>{this.state.isLogin ? '所爱隔山海，山海不可平' : ''}</Text>
              <View style={{ flexDirection: 'row', justifyContent: 'space-between', marginTop: adapt(10), marginHorizontal: adapt(15) }}>
                {data.map((item, index) => <TouchableOpacity key={index} style={{ flex: 1, alignItems: 'center' }}>
                  <Text style={{ color: '#fff', fontSize: adapt(16) }}>{item.num}</Text>
                  <Text style={{ color: '#fff', marginTop: adapt(5), fontSize: adapt(16) }}>{item.name}</Text>
                </TouchableOpacity>)}
              </View>
            </TouchableOpacity>}
            parallaxHeaderHeight={250}>
            <View style={{ borderBottomWidth: StyleSheet.hairlineWidth, borderColor: '#dcdcdc' }}>
              {infoData.map((item, index) => <View key={index} style={{ backgroundColor: '#fff' }}>
                {(index == 4 || index == 0) && <View style={styles.cellStyle} />}
                <TouchableOpacity onPress={item.onPress} activeOpacity={.8} style={[styles.itemCell, (index == 3 || index == 5) && { borderBottomWidth: 0 }]}>
                  <Text style={{ color: '#333', fontSize: adapt(16) }}>{item.title}</Text>
                  <Image source={Images.other.icon_jinru} style={styles.rightIcon} />
                </TouchableOpacity>
              </View>)}
            </View>
          </ParallaxScrollView>
        </View>
        <Text onPress={() => { Toast.success(this.state.isLogin ? '已退出' : '已登录'); this.setState({ isLogin: !this.state.isLogin }); }} style={{ alignSelf: 'center', color: '#000', padding: adapt(10), fontSize: adapt(16) }}>登录切换</Text>
      </View>
    );
  }
}

const styles = StyleSheet.create({
  container: {
    flex: 1,
    backgroundColor: '#ededed',
  },
  // image: {
  //   borderWidth: 1,
  //   width: 300,
  //   height: 100,
  //   borderRadius: 5,
  //   borderColor: '#ccc'
  // },
  // img: {
  //   height: 98,
  //   width: 300,
  // },
  // saveImg: {
  //   height: 30,
  //   padding: 6,
  //   textAlign: 'center',
  //   backgroundColor: '#3BC1FF',
  //   color: '#FFF',
  //   marginTop: 10,
  // },
  infoCellStyle: {
    flexDirection: 'row',
    backgroundColor: '#fff',
    alignItems: 'center',
    marginBottom: adapt(8),
    borderBottomWidth: StyleSheet.hairlineWidth,
    borderColor: '#d7d7d7'
  },
  cellStyle: {
    height: adapt(10),
    backgroundColor: '#ededed',
    borderBottomWidth: StyleSheet.hairlineWidth,
    borderColor: '#dcdcdc',
    borderTopWidth: StyleSheet.hairlineWidth
  },
  itemCell: {
    height: adapt(50),
    marginLeft: adapt(18),
    paddingRight: adapt(18),
    alignItems: 'center',
    flexDirection: 'row',
    justifyContent: 'space-between',
    borderBottomWidth: StyleSheet.hairlineWidth,
    borderColor: '#dcdcdc',
    backgroundColor: '#fff'
  },
  rightIcon: {
    width: adapt(20),
    height: adapt(20),
    tintColor: '#b1b1b1',
    marginLeft: adapt(5)
  },
  infoViewStyle: {
    width: width - adapt(120),
    flexDirection: 'row',
    justifyContent: 'space-between',
    alignItems: 'center'
  },
  avatarStyle: {
    marginTop: isIphoneX() ? adapt(45) : isIOS ? adapt(25) : adapt(5),
    width: adapt(80),
    height: adapt(80),
    borderRadius: adapt(40)
  },
  gridCellStyle: {
    flex: 1,
    flexDirection: 'row',
    flexWrap: 'wrap',
    borderTopWidth: StyleSheet.hairlineWidth,
    borderColor: '#d7d7d7'
  },
  gridItemStyle: {
    flexDirection: 'row',
    height: adapt(90),
    backgroundColor: '#fff',
    width: width / 2 - StyleSheet.hairlineWidth,
    borderBottomWidth: StyleSheet.hairlineWidth,
    borderColor: '#d7d7d7',
    alignItems: 'center'
  },
  gridImageStyle: {
    width: adapt(45),
    height: adapt(45),
    marginLeft: adapt(15),
    marginRight: adapt(10)
  }
});

MinePage.proptypes = {
  task: PropTypes.object,
  dispatch: PropTypes.func
}
function mapStateToProps({ task }) {
  return { task }
}
export default connect(mapStateToProps)(MinePage)
